//
//  UICoverFlowLayout.m
//  Topkea
//
//  Created by 周良飞 on 15/5/5.
//  Copyright (c) 2015年 e360. All rights reserved.
//

#import "TMCoverFlowSpace.h"

@interface TMCoverFlowSpace()
@end
//
@implementation TMCoverFlowSpace

-(CGSize)collectionViewContentSize {
    CGFloat width = 0;
    NSInteger sections = self.collectionView.numberOfSections;
    for (NSInteger i = 0; i<sections; i++) {
        NSInteger items = [self.collectionView numberOfItemsInSection:i];
        width += self.expand + (self.itemSize.width + self.space) * items - self.space;
    }
    return CGSizeMake(width, self.collectionView.bounds.size.height);
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}
//
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    //全部
    NSMutableArray *allIndexPaths=[NSMutableArray array];
    NSInteger sections = self.collectionView.numberOfSections;
    for (NSInteger i = 0; i<sections; i++) {
        NSInteger items = [self.collectionView numberOfItemsInSection:i];
        for (NSInteger j = 0; j<items; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [allIndexPaths addObject:indexPath];
        }
    
    }
    //可视区
    CGSize size = self.collectionView.bounds.size;
    CGPoint offset = self.collectionView.contentOffset;
    CGRect visible = CGRectMake(offset.x, offset.y, size.width, size.height);
    //
    NSMutableArray *resultAttributes=[NSMutableArray array];
    for(NSIndexPath *indexPath in allIndexPaths){
        UICollectionViewLayoutAttributes *attribute = [self layoutAttributesForItemAtIndexPath:indexPath];
        if (CGRectContainsRect(visible, attribute.frame) || CGRectIntersectsRect(visible, attribute.frame)) {
            [resultAttributes addObject:attribute];
        }
    }
    return [NSArray arrayWithArray:resultAttributes];
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat width = self.degree;
    for (NSInteger i = 0; i<indexPath.section; i++) {
        NSInteger items = [self.collectionView numberOfItemsInSection:i];
        width += self.expand + (self.itemSize.width + self.space) * items - self.space;
    }
    [attribute setFrame:CGRectMake(width+indexPath.row*(self.itemSize.width + self.space), self.headerReferenceSize.height, self.itemSize.width, self.itemSize.height)];

    //
    return attribute;
}

//- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
//    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
//    CGFloat width = 30;
//    CGFloat _width = (self.itemSize.width + self.space) * [self.collectionView numberOfItemsInSection:indexPath.section] - self.space;
//    for (NSInteger i = 0; i<indexPath.section; i++) {
//        NSInteger items = [self.collectionView numberOfItemsInSection:i];
//        width += self.expand + (self.itemSize.width + self.space) * items - self.space;
//    }
//    [attribute setFrame:CGRectMake(width, 0, _width, self.headerReferenceSize.height)];
//    return attribute;
//}
@end